package com.dongdongshop.serviceimpl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.pojo.TbOrder;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.pojo.TbUserScoreAccount;
import com.dongdongshop.pojo.ToTxService;
import com.dongdongshop.service.TbOrderService;
import com.dongdongshop.service.TbUserScoreAccountservice;
import com.dongdongshop.vo.TbOrderAndTbOrderItemVO;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

import java.math.BigDecimal;

@Service
@org.springframework.stereotype.Service
public class ToTxServiceImpl implements ToTxService {
    @Reference
    private TbOrderService tbOrderService;
    @Reference
    private TbUserScoreAccountservice tbUserScoreAccountservice;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    //发送mq消息
    @Override
    public void sendMessage(String out_trade_no, String trade_no, String status, String amount) {

        TbOrderAndTbOrderItemVO vo = new TbOrderAndTbOrderItemVO();
        TbOrder tbOrder = new TbOrder();
        tbOrder.setOutTradeNo(out_trade_no);
        tbOrder.setTradeNo(trade_no);//增加流水号
        tbOrder.setStatus(status);//将支付状态改为已支付

        TbOrderItem tbOrderItem = new TbOrderItem();
        tbOrderItem.setOutTradeNo(out_trade_no);
        tbOrderItem.setTradeNo(trade_no);//增加流水号

        vo.setTbOrder(tbOrder);
        vo.setTbOrderItem(tbOrderItem);
        //根据订单获取用户id
        String userId = tbOrderService.selectUserId(out_trade_no);
        //创建积分类对象
        TbUserScoreAccount tsa = new TbUserScoreAccount();
        //将需要消费的信息加入到类中
        tsa.setIntegral(amount);//分数（消费金额）
        tsa.setUserid(userId);//用户名
        //将积分类对象转为message
        Message message = MessageBuilder.withPayload(tsa).build();
        TransactionSendResult result = rocketMQTemplate.sendMessageInTransaction("tx-group","tx-integral",message,vo);



    }
}
